﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

namespace LongNet.Util
{
    /// <summary>
    /// Provides a set of static methods for extensions.
    /// </summary>
    public static partial class NameValueCollectionExtensions
    {
        #region Static Methods

        /// <summary>
        /// Gets the collection value by the specified name.
        /// </summary>
        /// <typeparam name="T">must be basic type</typeparam>
        /// <param name="collection"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T Get<T>(this NameValueCollection collection, string name)
        {
            if (typeof(T) == typeof(string))
                return Get<T>(collection, name, (T)Convert.ChangeType(string.Empty, typeof(T)));
            else
                return Get<T>(collection, name, default(T));
        }

        /// <summary>
        /// Gets the collection value by the specified name.
        /// </summary>
        /// <typeparam name="T">must be basic type</typeparam>
        /// <param name="collection"></param>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T Get<T>(this NameValueCollection collection, string name, T defaultValue)
        {
            try
            {
                return (T)Convert.ChangeType(GetString(collection, name), typeof(T));
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// Gets the collection string value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static string GetString(this NameValueCollection collection, string name)
        {
            object val = collection[name];
            return val == null ? string.Empty : val.ToString();
        }

        /// <summary>
        /// Gets the collection byte value by the specified name. 
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static byte GetByte(this NameValueCollection collection, string name)
        {
            return Utils.StringToByte(GetString(collection, name), 0);
        }
        /// <summary>
        /// Gets the collection byte value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static byte GetByte(this NameValueCollection collection, string name, byte defaultValue)
        {
            return Utils.StringToByte(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection int value by the specified name.
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static int GetInt(this NameValueCollection collection, string name)
        {
            return Utils.StringToInt(GetString(collection, name), 0);
        }
        /// <summary>
        /// Gets the collection int value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static int GetInt(this NameValueCollection collection, string name, int defaultValue)
        {
            return Utils.StringToInt(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection long value by the specified name.
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static long GetLong(this NameValueCollection collection, string name)
        {
            return Utils.StringToLong(GetString(collection, name), 0);
        }
        /// <summary>
        /// Gets the collection long value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static long GetLong(this NameValueCollection collection, string name, long defaultValue)
        {
            return Utils.StringToLong(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection float value by the specified name.
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static float GetFloat(this NameValueCollection collection, string name)
        {
            return Utils.StringToFloat(GetString(collection, name), 0F);
        }
        /// <summary>
        /// Gets the collection float value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static float GetFloat(this NameValueCollection collection, string name, float defaultValue)
        {
            return Utils.StringToFloat(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection double value by the specified name.
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static double GetDouble(this NameValueCollection collection, string name)
        {
            return Utils.StringToDouble(GetString(collection, name), 0D);
        }
        /// <summary>
        /// Gets the collection double value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static double GetDouble(this NameValueCollection collection, string name, double defaultValue)
        {
            return Utils.StringToDouble(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection decimal value by the specified name.
        /// If the request name does not exist, 0 will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static decimal GetDecimal(this NameValueCollection collection, string name)
        {
            return Utils.StringToDecimal(GetString(collection, name), 0M);
        }
        /// <summary>
        /// Gets the collection decimal value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static decimal GetDecimal(this NameValueCollection collection, string name, decimal defaultValue)
        {
            return Utils.StringToDecimal(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection bool value by the specified name.
        /// If the request name does not exist, false will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static bool GetBool(this NameValueCollection collection, string name)
        {
            return Utils.StringToBool(GetString(collection, name), false);
        }
        /// <summary>
        /// Gets the collection bool value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static bool GetBool(this NameValueCollection collection, string name, bool defaultValue)
        {
            return Utils.StringToBool(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection DateTime value by the specified name.
        /// If the request name does not exist, DateTime.MinValue will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static DateTime GetDateTime(this NameValueCollection collection, string name)
        {
            return Utils.StringToDateTime(GetString(collection, name), DateTime.MinValue);
        }
        /// <summary>
        /// Gets the collection DateTime value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static DateTime GetDateTime(this NameValueCollection collection, string name, DateTime defaultValue)
        {
            return Utils.StringToDateTime(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection Guid value by the specified name.
        /// If the request name does not exist, Guid.Empty will be returned.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <returns>The request value.</returns>
        public static Guid GetGuid(this NameValueCollection collection, string name)
        {
            return Utils.StringToGuid(GetString(collection, name), Guid.Empty);
        }
        /// <summary>
        /// Gets the collection Guid value by the specified name.
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static Guid GetGuid(this NameValueCollection collection, string name, Guid defaultValue)
        {
            return Utils.StringToGuid(GetString(collection, name), defaultValue);
        }

        /// <summary>
        /// Gets the collection enum value by the specified name, ignore case.
        /// If the request name does not exist, false will be returned.
        /// </summary>
        /// <typeparam name="T">An enum type.</typeparam>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <returns>The request value.</returns>
        public static T GetEnum<T>(this NameValueCollection collection, string name, T defaultValue)
        {
            return Utils.StringToEnum(GetString(collection, name), defaultValue, true);
        }
        /// <summary>
        /// Gets the collection enum value by the specified name.
        /// If the request name does not exist, false will be returned.
        /// </summary>
        /// <typeparam name="T">An enum type.</typeparam>
        /// <param name="collection"></param>
        /// <param name="name">The name to search.</param>
        /// <param name="defaultValue">If the request name does not exist, defaultValue will be returned.</param>
        /// <param name="ignoreCase">If true, ignore case; otherwise, regard case.</param>
        /// <returns>The request value.</returns>
        public static T GetEnum<T>(this NameValueCollection collection, string name, T defaultValue, bool ignoreCase)
        {
            return Utils.StringToEnum(GetString(collection, name), defaultValue, ignoreCase);
        }

        #endregion
    }
}
